package com.hepinghospital.user.ui.activity.consult;

import android.os.Bundle;

import com.hepinghospital.user.R;
import com.hepinghospital.user.base.BaseActivity;
import com.hepinghospital.user.eventbus.WebSocketWrapper;
import com.hepinghospital.user.ui.activity.nursing.NurseConsultChatActivity;
import com.hepinghospital.user.ui.dialog.ChooseDialog;
import com.hepinghospital.user.util.ToastUtil;
import com.rest.business.RestProxy;
import com.rest.observer.BaseObserver;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import jz.joyoung.robot.util.UIConstants;


/**
 * Created by HP on 2020/3/14.
 */

public class DialogActivity extends BaseActivity {

    WebSocketWrapper obj;

    @Override
    protected void initViews() {
        showLogoutDialog();
    }

    @Override
    protected void initData() {
        obj = (WebSocketWrapper) getIntent().getSerializableExtra("wsObj");
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_dialog;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent2(WebSocketWrapper webSocket) {
        if (webSocket.cmd == 101) {
            showLogoutDialog();
        }
    }

    private ChooseDialog mChooseDialog;

    private void showLogoutDialog() {
        StringBuilder dialogContent = new StringBuilder();
        if (obj.cmd == UIConstants.WEBSOCKET_CMD_101){
            dialogContent.append(getString(R.string.string_doctor));
        } else if (obj.cmd == UIConstants.WEBSOCKET_CMD_209) {
            dialogContent.append(getString(R.string.string_nurse));
        }
        dialogContent.append(getString(R.string.end_consult_content));

        mChooseDialog = ChooseDialog.newInstance();
        mChooseDialog.show(getSupportFragmentManager(), "chatFinish");
        mChooseDialog.setText(dialogContent.toString(), getString(R.string.string_confirm_end), getString(R.string.string_continue_consult));
        mChooseDialog.setmListener(new ChooseDialog.OnItemClickListener() {

            @Override
            public void onConfirm() {
                if (obj.cmd == UIConstants.WEBSOCKET_CMD_101){
                    updateDiagnoseStatus(obj.from, obj.diagnoseId, 1);
                }else if (obj.cmd == UIConstants.WEBSOCKET_CMD_209){
                    updatePatientDiagnoseStatus(obj.diagnoseId, 1);

                }
            }

            @Override
            public void onCancel() {
                if (obj.cmd == UIConstants.WEBSOCKET_CMD_101){
                    updateDiagnoseStatus(obj.from, obj.diagnoseId, 0);
                }else if (obj.cmd == UIConstants.WEBSOCKET_CMD_209){
                    updatePatientDiagnoseStatus(obj.diagnoseId, 0);

                }
            }
        });
    }

    private void updateDiagnoseStatus(String doctorUserId, String diagnoseId, final int status) {
        RestProxy.getInstance().updateDiagnoseStatus(doctorUserId, diagnoseId, status + "", new BaseObserver<String>() {
            @Override
            public void onSuccess(String s) {
                if (status == 1) {
                    WebSocketWrapper webSocket = new WebSocketWrapper();
                    webSocket.cmd = 100;
                    EventBus.getDefault().postSticky(webSocket);
                    mChooseDialog.dismiss();
                    finish();
                    Bundle bundle = new Bundle();
                    bundle.putString("diagnoseId", diagnoseId);
                    startActivity(DiagnoseOrderDetailActivity.class, bundle);
                } else if (status == 0) {
                    mChooseDialog.dismiss();
                    finish();
                }
            }

            @Override
            public void onError(Throwable e) {
                ToastUtil.onError(DialogActivity.this, e);
            }

        });
    }



    private void updatePatientDiagnoseStatus(String diagnoseId, final int status) {
        RestProxy.getInstance().updatePatientDiagnoseStatus(diagnoseId, status + "", new BaseObserver<String>() {
            @Override
            public void onSuccess(String s) {
                mChooseDialog.dismiss();
                finish();
                if (NurseConsultChatActivity.finishActivityInterface != null){
                    NurseConsultChatActivity.finishActivityInterface.finishNurseConsultChatActivity();
                }
            }

            @Override
            public void onError(Throwable e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ToastUtil.onError(DialogActivity.this, e);

                    }
                });
            }

        });
    }
}
